﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Product;
using BrokerBase.Lib.Base;
using BrokerBase.Lib.Tool;

namespace BrokerBase.Lib.Policy
{
    public class PolicyLight:BasePolicy, IPolicyLight
    {
        private string productName;

        private IPolicy policy;

        public PolicyLight()
            : base()
        {
        }

        public PolicyLight(IPolicy policy)
            : this()
        {
            this.Policy = policy;
            if (policy != null)
            {
                this.Owner = policy.Owner;
                this.ProductType = policy.ProductType;
            }
        }

        public PolicyLight(IScheme scheme, IPolicy policy)
            : this(policy)
        {
            this.Owner = scheme;
        }

        //public virtual IScheme Owner { get; set; }

        public virtual IPolicy Policy 
        {
            get { return this.policy; }
            set
            {
                this.policy = value;
                this.ProductType = this.policy != null ? this.policy.ProductType : this.ProductType;
            }
        }

        public virtual string ProductName 
        {
            get
            {
                string name;
                if (policy != null && policy.Product != null)
                {
                    name = policy.Product.Name;
                }
                else
                {
                    name = this.productName;
                }

                return name;
            }
            set
            {
                this.productName = value;
            }
        }

        public virtual InsurancePeriodStyle InsurancePeriodStyle { get; set; }

        public virtual int InsurancePeriodValue { get; set; }

        public virtual PaymentPeriodStyle PaymentPeriodStyle { get; set; }

        public virtual int PaymentPeriodValue { get; set; }

        //public virtual ITimeUnit TimeUnit { get; set; }

        public virtual UnitType UnitType { get; set; }

        //public virtual double Amount { get; set; }




        public virtual int Id { get; set; }

        public virtual string InsurancePeriodDescription { get; set; }

        public virtual string PaymentPeriodDescription { get; set; }


        public virtual void FromPolicy()
        {
            InsurancePeriodDescription = PolicyUtil.GetInsurancePeriodDescription(this);
            PaymentPeriodDescription = PolicyUtil.GetPaymentPeriodDescription(this);

            if (Policy != null)
            {

                ProductName = Policy.Product.Name;
                ProductType = Policy.Product.ProductType;

                if (Policy.SelectedInsurancePeriod.Style == InsurancePeriodStyle.Age || Policy.SelectedInsurancePeriod.Style == InsurancePeriodStyle.Period)
                {
                    InsurancePeriodStyle = Policy.SelectedInsurancePeriod.Style;
                    InsurancePeriodValue = Policy.SelectedInsurancePeriod.Value * Policy.OverInsurancePeriod;
                }
                else
                {
                    InsurancePeriodStyle = InsurancePeriodStyle.Period;
                    InsurancePeriodValue = Policy.InsurancePeriodLength;
                }

                if (Policy.SelectedPaymentPeriod.Style == PaymentPeriodStyle.Age || Policy.SelectedPaymentPeriod.Style == PaymentPeriodStyle.Period)
                {
                    PaymentPeriodStyle = Policy.SelectedPaymentPeriod.Style;
                    PaymentPeriodValue = Policy.SelectedPaymentPeriod.Value * Policy.OverPaymentPeriod;
                }
                else
                {
                    PaymentPeriodStyle = PaymentPeriodStyle.Period;
                    PaymentPeriodValue = Policy.PaymentPeriodLength;
                }


                SelectedTimeUnit = Policy.SelectedTimeUnit;
                UnitType = Policy.Product.UnitType;
                Amount = Policy.Amount;
                Fee = Policy.Fee;

            }

            TotalFee = PaymentPeriodLength * Fee;
        }

        public override object Clone()
        {
            PolicyLight tmp = new PolicyLight();

            BaseTools.CopyTo<PolicyLight>(this, tmp);

            return tmp;
        }
    }
}
